<?php

class Signup extends MY_Controller{
    public function __construct() 
    {
        parent::__construct();

    } 
        
    function index(){
        $member = array(
                                'username' => '',
                                'password' => '',
                                'confirmPwd' => '',
                                'idCard' => '',
                                'firstname' => '',
                                'lastname' => '',
                                'day' => '',
                                'month' => '',
                                'year' => '',
                                'address' => '',
                                'mobile' => '',
                                'email' => '',
        );
        $this->data['member'] = $member;
        
        for($i=0;$i<=30;$i++):
            $days[$i+1] = $i + 1;             
        endfor;

        $months = array(1 => "Jan",
                        2 => "Feb",
                        3 => "Mar",
                        4 => "Apr",
                        5 => "May",
                        6 => "Jun",
                        7 => "Jul",
                        8 => "Aug",
                        9 => "Sep",
                        10 => "Oct",
                        11 => "Nov",
                        12 => "Dec");

        $time=time();
        for($i=0;$i<=100;$i++):
            $years[date('Y',$time) - $i] = date('Y',$time) - $i;
        endfor;

        $username = array(
              'id'        => 'signup_username',
              'name'        => 'signup_username'
            );
        
        $password = array(
                'id'        => 'signup_password',
              'name'        => 'signup_password'
            );
        
        $confirmPwd = array(
              'id'        => 'signup_confirmPwd',
              'name'        => 'signup_confirmPwd'
            );
        
        $idCard = array(
              'id'        => 'signup_idCard',
              'name'        => 'signup_idCard'
            );
        
        $firstname = array(
              'id'        => 'signup_firstname',
              'name'        => 'signup_firstname'
            );
        
        $lastname = array(
              'id'        => 'signup_lastname',
              'name'        => 'signup_lastname'
            );
        
        $day = 'class="styledselect-day" id="d"' ;
        $month = 'class="styledselect-month" id="m"' ;
        $year = 'class="styledselect-year" id="y"' ;
        
        $address = array(
              'id'        => 'signup_address',
              'name'        => 'signup_address'
            );
        
        $mobile = array(
              'id'        => 'signup_mobile',
              'name'        => 'signup_mobile'
            );
        
        $email = array(
              'id'        => 'signup_email',
              'name'        => 'signup_email'
            );
        
        $this->data['username'] = $username;
        $this->data['password'] = $password;
        $this->data['confirmPwd'] = $confirmPwd;
        $this->data['idCard'] = $idCard;
        $this->data['firstname'] = $firstname;
        $this->data['lastname'] = $lastname;
        $this->data['day'] = $day;
        $this->data['month'] = $month;
        $this->data['year'] = $year;
        $this->data['address'] = $address;
        $this->data['mobile'] = $mobile;
        $this->data['email'] = $email;
        
        $this->data['days'] = $days;
        $this->data['months'] = $months;
        $this->data['years'] = $years;

        //get id card all user
        $this->load->model("Member_Model");
        $memberList = $this->Member_Model->getMemberSelectList();
        $arrIdCard = array();
        foreach($memberList as $member){
            array_push($arrIdCard, $member->ID_CARD);
        }

        $this->data['idCardList'] = $arrIdCard;
        $this->data['main_content'] = 'signup_test';
        $this->load->view('includes/template', $this->data);
    }
    
    function addMember(){
        $signup = array(
			'USERNAME' => $this->input->post('signup_username'),
                        'PASSWORD' => MD5($this->input->post('signup_password')),
			'FIRSTNAME' => $this->input->post('signup_firstname'),
			'LASTNAME' => $this->input->post('signup_lastname'),
			'DOB' => $this->input->post('signup_year').'-'.$this->input->post('signup_month').'-'.$this->input->post('signup_day'),
                        'ID_CARD' => $this->input->post('signup_idCard'),
			'ADDRESS' => $this->input->post('signup_address'),
			'MOBILE' => $this->input->post('signup_mobile'),
                        'EMAIL' => $this->input->post('signup_email'),
			'USER_TYPE' => 2
		);
        
        $memberMenuList = array(1,7);
        
        $this->load->model('Member_Model');
        $this->Member_Model->addMember($signup, $memberMenuList);
        
        $this->data['main_content'] = 'home';
        $this->load->view('includes/template', $this->data);
    }
    
    function getMember($action, $memberID) {
//        echo urlencode($memberIDEncode);
        $this->load->Model('Member_Model');
        $query = $this->Member_Model->getMemberByID($memberID);
        
        if($query->num_rows() == 1) // if the user's credentials validated...
        {
            $row = $query->row();
            
            $dob = $row->DOB;
            $day   = intval(date('d', strtotime($dob)));
            $month = intval(date('m', strtotime($dob)));
            $year  = date('Y', strtotime($dob));

            $member = array(
                                    'id' => $row->ID,
                                    'username' => $row->USERNAME,
                                    'password' => $row->PASSWORD,
                                    'confirmPwd' => $row->PASSWORD,
                                    'idCard' => $row->ID_CARD,
                                    'firstname' => $row->FIRSTNAME,
                                    'lastname' => $row->LASTNAME,
                                    'day' => $day,
                                    'month' => $month,
                                    'year' => $year,
                                    'address' => $row->ADDRESS,
                                    'mobile' => $row->MOBILE,
                                    'email' => $row->EMAIL,
            );
            $this->data['member'] = $member;
        }
        
        for($i=0;$i<=30;$i++):
            $days[$i+1] = $i + 1;             
        endfor;

        $months = array(1 => "Jan",
                        2 => "Feb",
                        3 => "Mar",
                        4 => "Apr",
                        5 => "May",
                        6 => "Jun",
                        7 => "Jul",
                        8 => "Aug",
                        9 => "Sep",
                        10 => "Oct",
                        11 => "Nov",
                        12 => "Dec");

        $time=time();
        for($i=0;$i<=100;$i++):
            $years[date('Y',$time) - $i] = date('Y',$time) - $i;
        endfor;

        $this->data['days'] = $days;
        $this->data['months'] = $months;
        $this->data['years'] = $years;
        
        $username = array(
              'disabled'    => 'diabled',
              'name'        => 'signup_username'
            );
        
        $password = array(
              'disabled'    => 'diabled',
              'name'        => 'signup_password'
            );
        
        $confirmPwd = array(
              'disabled'    => 'diabled',
              'name'        => 'signup_confirmPwd'
            );
        
        $idCard = array(
              'disabled'    => 'diabled',
              'name'        => 'signup_idCard'
            );
        
        $firstname = array(
              'disabled'    => 'diabled',
              'name'        => 'signup_firstname'
            );
        
        $lastname = array(
              'disabled'    => 'diabled',
              'name'        => 'signup_lastname'
            );
        
        $day = 'class="styledselect-day" id="d" disabled="disabled"' ;
        $month = 'class="styledselect-month" id="m" disabled="disabled"' ;
        $year = 'class="styledselect-year" id="y" disabled="disabled"' ;
        
        $address = array(
              'disabled'    => 'diabled',
              'name'        => 'signup_address'
            );
        
        $mobile = array(
              'disabled'    => 'diabled',
              'name'        => 'signup_mobile'
            );
        
        $email = array(
              'disabled'    => 'diabled',
              'name'        => 'signup_email'
            );
        
        $this->data['username'] = $username;
        $this->data['password'] = $password;
        $this->data['confirmPwd'] = $confirmPwd;
        $this->data['idCard'] = $idCard;
        $this->data['firstname'] = $firstname;
        $this->data['lastname'] = $lastname;
        $this->data['day'] = $day;
        $this->data['month'] = $month;
        $this->data['year'] = $year;
        $this->data['address'] = $address;
        $this->data['mobile'] = $mobile;
        $this->data['email'] = $email;
        
        //get all course by member
        $this->load->Model('Course_Permission_Model');
        $allCourseList = $this->Course_Permission_Model->getAllCourseByMemberID($memberID);
        for($i=0;$i<count($allCourseList);$i++):
            if($allCourseList[$i]->TIMES <= 0):
               unset($allCourseList[$i]);
            endif;
        endfor;
        $this->data['allCourseList'] = $allCourseList;
        
        $this->data['action'] = $action;
        $this->data['main_content'] = 'signup_test';
        $this->load->view('includes/template', $this->data);
    }
    
    function Approve($memberID){
        $this->load->model('Member_Model');
        $this->load->model('Course_Permission_Model');
        $this->load->model('Course_Model');
        $this->load->model('Menu_Model');
        
        //approve member
        $memberObj = array('APPROVE_FLAG' => true);
        
        //get all course's times from member
//        $times = 0;
//        $courseList = $this->Course_Permission_Model->getCourseByMemberID($memberID);
//        foreach($courseList as $course):
//           $result = $this->Course_Model->getVideoTimesByCourseID($course->COURSE_ID);
//           $times = $times + $result->VIDEOS;
//        endforeach;
        
        //get all member menu
        //$memberMenu = $this->Menu_Model->getMenuAndSubMenuByID(3);
        $memberMenu = array(1,6,7);
        
        $this->Member_Model->approveMember($memberID, $memberObj, $memberMenu);
        
        redirect('Approve');
    }
}
?>
